const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const fileUpload = require('express-fileupload');
const path = require('path');

// 路由
const authRoutes = require('./routes/authRoutes');
const merchantRoutes = require('./routes/merchantRoutes');
const storeRoutes = require('./routes/storeRoutes');
const userRoutes = require('./routes/userRoutes');
const adminRoutes = require('./routes/adminRoutes');
const orderRoutes = require('./routes/orderRoutes');
const couponRoutes = require('./routes/couponRoutes');
const carouselRoutes = require('./routes/carouselRoutes');
const hotRecommendRoutes = require('./routes/hotRecommendRoutes');
const gasStationRoutes = require('./routes/gasStationRoutes');
const hotelRoutes = require('./routes/hotelRoutes');
const productRoutes = require('./routes/productRoutes');

// 中间件
const errorMiddleware = require('./middleware/errorMiddleware');

const app = express();
const port = process.env.PORT || 3000;

// 中间件配置
app.use(cors());
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }));

// 文件上传配置
app.use(fileUpload({
    limits: { fileSize: 50 * 1024 * 1024 }, // 50MB
    useTempFiles: true,
    tempFileDir: '/tmp/',
    createParentPath: true
}));

// 静态文件服务
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));

// API路由
app.use('/api/auth', authRoutes);
app.use('/api/merchant', merchantRoutes);
app.use('/api/stores', storeRoutes);
app.use('/api/user', userRoutes);
app.use('/api/admin', adminRoutes);
app.use('/api/orders', orderRoutes);
app.use('/api/coupons', couponRoutes);
app.use('/api/carousels', carouselRoutes);
app.use('/api/hot-recommends', hotRecommendRoutes);
app.use('/api/gas-stations', gasStationRoutes);
app.use('/api/hotels', hotelRoutes);
app.use('/api/products', productRoutes);

// 错误处理中间件
app.use(errorMiddleware);

// 启动服务器
app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});